package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import factory.ServiceFactory;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class Goods
 */
@WebServlet("/goods.do")
public class Goods extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String meth = request.getParameter("method");
		if (meth == null || meth.equals("")) {
			meth = "index";
		}
		switch (meth) {
		case "index":
			request.getRequestDispatcher("addGoods.jsp").forward(request, response);
			break;
		case "verify":
			getOneGoods(request, response);
			break;
		case "add":
			addGoods(request, response);
			break;
		default:
			break;
		}
	}

	protected void addGoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		JSONObject json = new JSONObject();
		String txm = request.getParameter("txm");
		String name = request.getParameter("name");
		String zklstring = request.getParameter("zkl");
		String djString = request.getParameter("dj");
		String slString = request.getParameter("sl");
		int zkl = Integer.valueOf(zklstring == null ? "0" : zklstring);
		float dj = Float.valueOf(djString == null ? "0" : djString);
		int sl = Integer.valueOf(slString == null ? "0" : slString);
		if (dj <= 0 || sl < 0) {
			json.put("status", 500);
			json.put("msg", "新货信息有误！");
		} else {
			domain.Goods goods = ServiceFactory.getSerInstance().getOneGoodsInfo(txm);
			boolean addOrUpdate = false;// 新加
			if (goods != null)// 更新
				addOrUpdate = true;
			else {
				goods = new domain.Goods();
			}
			goods.setTxm(txm);
			goods.setName(name);
			goods.setPrice(dj);
			goods.setZkl(zkl);
			if (addOrUpdate) {// 更新
				goods.setNumber(sl + goods.getNumber());
				ServiceFactory.getSerInstance().updateGoods(goods);
				json.put("status", 200);
				json.put("msg", "添加成功！");
			} else {// 新加
				goods.setNumber(sl);
				json.put("status", 200);
				json.put("msg", "添加成功！");
				try {
					ServiceFactory.getSerInstance().addGoods(goods);
				}catch (Exception e) {
					// TODO: handle exception
					json.put("status", 500);
					json.put("msg", "已有此货品，需等待片刻再整货！");
				}
			}
			
		}
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();

	}

	protected void getOneGoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String txm = request.getParameter("txm");
		domain.Goods goods = ServiceFactory.getSerInstance().getOneGoodsInfo(txm);
		response.setContentType("application/json;charset=utf-8");
		JSONObject json = new JSONObject();
		if (goods != null) {
			json.put("status", 200);
			json.put("name", goods.getName());
			json.put("dj", String.valueOf(goods.getPrice()));
			json.put("sl", goods.getNumber());
			json.put("zk", goods.getZkl());
		} else {
			json.put("status", 400);
		}
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}

}
